// JavaScript Document
var source_name = "";
var Input_edited = false;

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function SmoothBoxGoToUrl(url) {
	document.location = (url);	
}

function isgreaterthan(a,b) { return a>b; }

function issmallerthan(a,b) { return a<b; }

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function MostraMessaggio(messaggio,classe_css,divsource) {
	//msg_box
	//inner_msg
	source_name = divsource;
	var intmsg = document.getElementById('inner_msg');
	var alt = 0; 
	var wid = 0;
	var sizo = window.getScrollSize();
	alt=sizo.y;
	wid=sizo.x;
	if(intmsg!=null) {
		var img = "";
		if(classe_css=='js_spang')
			img = "<img src=\"/template/img/green.jpg\" align=\"middle\" /><br/>";
		else if(classe_css=='js_spanw')
			img = "<img src=\"/template/img/warn.gif\" align=\"middle\" /><br/>";
		else if(classe_css=='js_spanr')
			img = "<img src=\"/template/img/error.gif\" align=\"middle\" /><br/>";
			
		intmsg.innerHTML = "<span class=\"" + classe_css + "\">" + img + messaggio + "</span>";

		 var myMsg = $('msg_box');
		 var midX = (wid/2 - 300/2);
		 if(window.ie) 
		 	midX = midX -300;
		 myMsg.style.display ="block";		 
		 var fxStart = new Fx.Styles(myMsg, {
			 duration: 700,
			 wait: false,
			 transition: Fx.Transitions.Quad.easeOut
		 });
		 fxStart.start({
			 'left': [midX, midX],
			 'top': [0, (alt/2)],
			 'opacity' : [0,100]
		 });
		 var fx = new Fx.Styles(myMsg, {
			 duration: 1500,
			 wait: false,
			 transition: Fx.Transitions.Quad.easeOut
		 });
		 setTimeout(NascondiMsg,3000);
 	} else
		alert(msg);
	intmsg = null;
	CleanMessage();

}
function NascondiMsg() {
	 var myMsg = $('msg_box');
	 var fxEnd = new Fx.Styles(myMsg, {
		 duration: 1500,
		 wait: false,
		 transition: Fx.Transitions.Quad.easeOut
	 });
	 fxEnd.start({
		 'opacity' : [100,0]
	 });
}

function CleanMessage() {
	var jmsgconte = document.getElementById(source_name);
	if(jmsgconte!=null)
		jmsgconte.innerHTML = "";
}

function FlashMessage(message) {
	var msgboxer = $("main_msg");
	if(!msgboxer) { alert(message); return; }
	var alt = 0; 
	var wid = 0;
	var sizo = window.getScrollSize();
	alt=sizo.y;
	wid=sizo.x;
	msgboxer.setStyles({
					   	'border-size':'2px',
						'background':'#FFF',
						'display':"block",
						'position':'absolute',
						'top':250,
						'left': ((wid/2)-300) 
					});
	msgboxer.set('html',message);
	var effetti = new Fx.Tween(msgboxer);
	var fun = function() { effetti.start('opacity',0); };
	effetti.start('opacity', 1).chain(
		function() {								  
			fun.delay(3000);
		}
	); 
}

function QuickMessage(message) {
	var msgboxer = $("main_msg");
	if(!msgboxer) return;
	var alt = 0; 
	var wid = 0;
	var sizo = window.getScrollSize();
	alt=sizo.y;
	wid=sizo.x;
	msgboxer.setStyles({'border-size':'2px','background':'#FFF','display':"block",'position':'absolute','top':250, 'left': ((wid/2)-300) });
	msgboxer.set('html',message);
	var effetti = new Fx.Tween(msgboxer);
	effetti.start('opacity', 1).chain(
		function() {								  
			this.start('opacity',0);
		}
	);
}
function showModal(caption,url,width,height) {
	if(!url) return;
	if(!width || width=='') width=500;
	if(!height || height=='') height=300;	
	var ope = "?";
	if(url.toString().contains('?') ) ope = "&";
	TB_show( (!caption?'Modal box':caption),url+ope+"keepThis=true&TB_iframe=true&height="+height+"&width="+width);
}
function showModalInline(caption,inlineid,width,height) {
	if(!inlineid) return;
	if(!width || width=='') width=500;
	if(!height || height=='') height=300;	
	var ope = "?";
	var url = document.location;
	if(url.toString().contains('?') ) ope = "&";
	try {
	TB_show( (!caption?'Modal box':caption),url+ope+"keepThis=true&TB_inline=true&inlineId="+inlineid+"&height="+height+"&width="+width);
	} catch (_eR) { }
}
function closeModal() {
	try {
		TB_remove();
	} catch (_er) {}
}

function LockToolbar() {
	try {
	var cont = $('msg_box');
	var testa = $('adm_menu');
	if(!cont) return;
	if(!testa) return;	
	var oggetto = new Element('div');
	cont.adopt(oggetto);
	oggetto.set('html',' ');
	oggetto.setStyle('opacity',0.3);
	oggetto.setStyle('position','absolute');	
	oggetto.setStyle('z-index','9999999');	
	oggetto.setStyle('background-color','#e2e3e0');		
	oggetto.setStyle('width',testa.getSize().x);
	oggetto.setStyle('height',testa.getSize().y);	
	oggetto.setStyle('top',testa.getPosition().y);	
	oggetto.setStyle('left',testa.getPosition().x);	
	//msg_box
	//Edit Mode:
	var elementiInput = $$('#adm_body input','#adm_body select', '#adm_body textarea');
	var elementiMenu = $$('.adm_button a');	
	if(!elementiInput) return;
	if(!elementiMenu) return;	
	
	elementiInput.each(function (elemento) {
		var tipo = elemento.get('tag');
		if(!tipo) return;
		if(tipo.toString().toLowerCase() =='input') {
			// per input	
			elemento.addEvent('click', function() {
				Input_edited=true;											  
			});
			return;
		}
		
		if(tipo.toString().toLowerCase() =='textarea') {
			// per textarea	
			elemento.addEvent('click', function() {
				Input_edited=true;	
				//alert('mi cambi');
			});			
			return;
		}
		
		if(tipo.toString().toLowerCase() =='select') {
			// per select	
			elemento.addEvent('change', function() {
				Input_edited=true;											  
			});			
			return;
		}
	});
	
	elementiMenu.each(function(elemento) {
		var hrefo = null; 
		var clicko = null; 
		if(elemento.hasClass('smoothbox'))  {
			return;
		}
		
		clicko = (elemento.getAttribute('onclick'));
		hrefo  = (elemento.getAttribute('href'));
		if(hrefo) hrefo = unescape(hrefo);
		//REM LINE DA CICCIO PER RISOLVERE "UFFA IN CHROME"
		
		elemento.setAttribute('onclick','');
		elemento.setAttribute('href','#');	
		elemento.removeEvents();
		elemento.addEvent('click',function(e) {
		   evt = new Event(e);evt.stop();
			var conferm = false;
			var mioTorno = false;
			if(Input_edited) { 
				conferm = confirm('Sono state effettuate delle modifiche, sei sicuro di voler effettuare l\'operazione');
			} else
				conferm = true;
			if(conferm) {
				if(clicko) {
					clicko = clicko.toString().replace('javascript:','');
					if(clicko.toString().contains('return') ) {
						clicko = clicko.toString().replace('return','');
						mioTorno = eval(clicko);
					} else
						eval(clicko);
				}
				if(hrefo) {
					hrefo = hrefo.toString().replace('javascript:','');
					eval(hrefo);				
				}	
			}
			return mioTorno;
		});
		
	});
	}  catch (errore) {
		alert('errore' + errore);	
	}
}

function loadMenuss() {
	var menus = $$('#adm_menu a');
	if(!menus) return;
	if(menus.length==0) return;
	if(menus) {
		menus.each(function(elemento,i) {
			elemento.addEvent('mouseover', function() {
				QuickMessage(this.get('html'));										
			});
		});
	}		
}

loadMenuss();